﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using System.Text.RegularExpressions;
using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;

namespace DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects
{
    public class RibbonBar : WatiNBase
    {
        #region Constructors

        public RibbonBar(WatiNBase watinBase) : base(watinBase.IEInstance, watinBase.SiteUrl, watinBase.DBName) { }


        #endregion

        #region Public Properties

        #region Divs
        public Div RibbonBarWrapper
        {
            get { return IEInstance.Div(Find.ById("dnnCPWrapRight")); }
        }
        public Div HeaderDiv
        {
            get { return IEInstance.Div(Find.ByClass("dnnCPHeader dnnClear")); }
        }
        public Div AdminMenusDiv
        {
            get { return IEInstance.Div(Find.ById(s => s.EndsWith("_RibbonBar_adminMenus"))); }
        }
        public Div RibbonBarDiv
        {
            get { return IEInstance.Div(Find.ById(new Regex("dnn_(cp_)?RibbonBar_BodyPanel"))); }
        }
        public Div CommonTasksDiv
        {
            get { return IEInstance.Div(Find.ById(s => s.EndsWith("RibbonBar_CommonTasksPanel"))); }
        }
        public Div CurrentPageDiv
        {
            get { return IEInstance.Div(Find.ById(s => s.EndsWith("_RibbonBar_CurrentPagePanel"))); }
        }
        public Div RibbonBarHeaderDiv
        {
            get { return IEInstance.Div(Find.ById("dnn_RibbonBar_RB_Header")); }
        }
        public Div ModuleSelectDiv
        {
            get { return IEInstance.Div(Find.ById(new Regex(@"dnn_(cp_)?RibbonBar_AddMod_ctl\d\d_DropDown"))); }
        }
        public Div InsertSelectDiv
        {
            get { return IEInstance.Div(Find.ById("dnn_RibbonBar_AddMod_PositionLst_DropDown")); }
        }
        public Div VisibilitySelectDiv
        {
            get { return IEInstance.Div(Find.ById("dnn_RibbonBar_AddMod_VisibilityLst_DropDown")); }
        }
        public Div ExistingPageSelectDiv
        {
            get { return IEInstance.Div(Find.ById("dnn_RibbonBar_AddMod_PageLst_DropDown")); }
        }
        public Div ExistingModuleSelectDiv
        {
            get { return IEInstance.Div(Find.ById(new Regex(@"dnn_(cp_)?RibbonBar_AddMod_ctl\d\d_DropDown"))); }
        }
        public Div MoveLocationSelectDiv
        {
            get { return IEInstance.Div(Find.ById("dnn_RibbonBar_EditPage_LocationLst_DropDown")); }
        }
        public Div MovePageSelectDiv
        {
            get { return IEInstance.Div(Find.ById("dnn_RibbonBar_EditPage_PageLst_DropDown")); }
        }
        public Div SkinSelectDiv
        {
            get { return IEInstance.Div(Find.ById(s => s.EndsWith("RibbonBar_EditPage_SkinLst_DropDown"))); }
        }
        public Div SiteSelectDiv
        {
            get { return IEInstance.Div(Find.ById("dnn_RibbonBar_SwitchSite_SitesLst_DropDown")); }
        }
        #endregion

        #region Images
        public Image CommunityRibbonBarHeaderImage
        {
            get { return RibbonBarHeaderDiv.Image(Find.ByTitle("DotNetNuke Community Edition")); }
        }
        #endregion

        #region Spans
        public Span HostTabLink
        {
            get { return RibbonBarDiv.Span(Find.ByText("Host")); }
        }
        public Span AdminTabLink
        {
            get { return RibbonBarDiv.Span(Find.ByText("Admin")); }
        }
        
      
        #endregion

        #region TabLinks
        public Link CurrentPageTabLink
        {
            get { return RibbonBarDiv.Link(Find.ByText(s => s.Contains("Current Page"))); }
        }
        public Link CommonTasksTabLink
        {
            get { return RibbonBarDiv.Link(Find.ByText("Common Tasks")); }
        }
        public Link OtherToolsTabLink
        {
            get { return RibbonBarDiv.Link(Find.ByText("Other Tools")); }
        }
        #endregion

        #region Links
        public Link AddModuleButton
        {
            get { return CommonTasksDiv.Link(Find.ById(s => s.EndsWith("RibbonBar_AddMod_cmdAddModule"))); }
        }
        public Link SiteSettingsLink
        {
            get { return IEInstance.Div(Find.ById("dnn_RibbonBar_PageSite")).Link(Find.ByTitle("Site Settings")); }
        }
        public Link UsersLink
        {
            get { return IEInstance.Div(Find.ById("dnn_RibbonBar_PageSite")).Link(Find.ByTitle("Users")); }
        }
        public Link AddNewPageLinkAdminTab
        {
            get { return IEInstance.Div(Find.ById("dnn_RibbonBar_PageSite")).Link(Find.ByTitle("Create New Page")); }
        }
        public Link AdminConsoleLink
        {
            get { return AdminMenusDiv.Link(Find.ByText(s => s.Contains("Admin"))); }
        }
        public Link HostConsoleLink
        {
            get { return AdminMenusDiv.Link(Find.ByText(s => s.Contains("Host"))); }
        }
        public Link ModulesSectionLink
        {
            get { return AdminMenusDiv.Link(Find.ByText(s => s.Contains("Modules"))); }
        }
        public Link PagesSectionLink
        {
            get { return AdminMenusDiv.Link(Find.ByText(s => s.Contains("Pages"))); }
        }
        public Link ToolsSectionLink
        {
            get { return AdminMenusDiv.Link(Find.ByText(s => s.Contains("Tools"))); }
        }
        public Link HostFileManagerLink
        {
            get { return IEInstance.Div(Find.ById("dnn_RibbonBar_PageHostSystem")).Link(Find.ByTitle("File Manager")); }
        }
        public Link HostExtensionsLink
        {
            get { return IEInstance.Div(Find.ById("dnn_RibbonBar_PageHostSystem")).Link(Find.ByTitle("Extensions")); }
        }
        public Link NewPageLink
        {
            get { return CurrentPageDiv.Link(Find.ByTitle("Create New Page")); }
        }
        public Link HelpLink
        {
            get { return AdminMenusDiv.Link(Find.ByTitle("Online Help")); }
        }
        public Link DeletePageLink
        {
            get { return CurrentPageDiv.Link(Find.ByText(s => s.Contains("Delete"))); }
        }
        public Link CopyPageLink
        {
            get { return IEInstance.Link(Find.ByTitle("Copy Page")); }
        }
        public Link ExportPageLink
        {
            get { return AdminMenusDiv.Link(Find.ByTitle("Export Page")); }
        }
        public Link ImportPageLink
        {
            get { return IEInstance.Link(Find.ByTitle("Import Page")); }
        }
        //public Link PopUpConfirmation
        //{
        //    get { return IEInstance.Links.Filter(Find.ByClass(s => s.StartsWith("rwPopupButton")))[0]; }
        //}
        
        public Link ModuleComboBox
        {
            get { return IEInstance.Div(Find.ById(s => s.EndsWith("RibbonBar_AddMod_UpdateAddModule"))).Link(Find.ById(new Regex(@"dnn_(cp_)?RibbonBar_AddMod_ctl\d\d_Arrow"))); }
        }
        public Link InsertComboBox
        {
            get { return IEInstance.TableCell(Find.ByClass("AddMod3")).Link(Find.ById("dnn_RibbonBar_AddMod_PositionLst_Arrow")); }
        }
        public Link ModulePaneComboBox
        {
            get { return ControlPanel.Link(Find.ById("dnn_RibbonBar_AddMod_PaneLst_Arrow")); }
        }
        public Link ModuleVisibilityComboBox
        {
            get { return IEInstance.TableCell(Find.ByClass("AddMod2")).Link(Find.ById("dnn_RibbonBar_AddMod_VisibilityLst_Arrow")); }
        }
        public Link EditCurrentPageLink
        {
            get { return CurrentPageDiv.Link(Find.ByTitle("Page Settings")); }
        }
        public Link AddExistingModulePageComboBox
        {
            get { return IEInstance.TableCell(Find.ByClass("AddMod2")).Link(Find.ById("dnn_RibbonBar_AddMod_PageLst_Arrow")); }
        }
        public Link AddExistingModuleComboBox
        {
            get { return CommonTasksDiv.Link(Find.ById(new Regex(@"dnn_(cp_)?RibbonBar_AddMod_ctl\d\d_Arrow"))); }
        }
        public Link TemplateComboBox
        {
            get { return ControlPanel.Link(Find.ById("dnn_RibbonBar_AddPage_TemplateLst_Arrow")); }
        }
        public Link InsertPageLocationComboBox
        {
            get { return ControlPanel.Link(Find.ById("dnn_RibbonBar_AddPage_LocationLst_Arrow")); }
        }
        public Link InsertPageComboBox
        {
            get { return ControlPanel.Link(Find.ById("dnn_RibbonBar_AddPage_PageLst_Arrow")); }
        }
        public Link MoveLocationComboBox
        {
            get { return ControlPanel.Link(Find.ById("dnn_RibbonBar_EditPage_LocationLst_Arrow")); }
        }
        public Link MovePageComboBox
        {
            get { return ControlPanel.Link(Find.ById("dnn_RibbonBar_EditPage_PageLst_Arrow")); }
        }
        public Link PageSkinComboBox
        {
            get { return ControlPanel.Link(Find.ById(s => s.EndsWith("RibbonBar_EditPage_SkinLst_Arrow"))); }
        }
        
        public Link CopyPermissionsToChildrenLink
        {
            get { return ControlPanel.Link(Find.ByTitle("Copy Permissions to Children")); }
        }
        public Link CopyDesignToChildrenLink
        {
            get { return CurrentPageDiv.Link(Find.ByTitle("Copy Skin and Container Settings to Children")); }
        }
        public Link SupportLink
        {
            get { return RibbonBarDiv.Link(Find.ByTitle("Support Tickets")); }
        }
        public Link PagesLink
        {
            get { return AdminMenusDiv.ListItem(Find.ById("dnnCurrentPage")).Link(Find.ByText(s => s.Contains("Pages"))); }
        }
        public Link AddPageLink
        {
            get { return ControlPanel.Link(Find.ByTitle("Add Page")); }
        }
        public Link UpdatePageLink
        {
            get { return ControlPanel.Link(Find.ByTitle("Update Page")); }
        }
        public Link SwitchSiteComboBox
        {
            get { return ControlPanel.Link(Find.ById("dnn_RibbonBar_SwitchSite_SitesLst_Arrow")); }
        }
        #endregion

        #region TextField
        public TextField ModuleTitleField
        {
            get { return IEInstance.TextField(Find.ById(s => s.EndsWith("_RibbonBar_AddMod_Title"))); }
        }
        public TextField NewPageName
        {
            get { return ControlPanel.TextField(Find.ById(s => s.EndsWith("_RibbonBar_AddPage_Name"))); }
        }
        public TextField EditPageNameField
        {
            get { return ControlPanel.TextField(Find.ById(s => s.EndsWith("_RibbonBar_EditPage_Name"))); }
        }
        #endregion

        #region Buttons
        public Button SwitchSiteButton
        {
            get { return ControlPanel.Button(Find.ById("dnn_RibbonBar_SwitchSite_cmdSwitch")); }
        }
        public Button PopUpConfirmation
        {
            get { return IEInstance.Div(Find.ByClass("ui-dialog-buttonset")).Button(Find.ByText("Yes")); }
        }
        public Button PopUpCancelation
        {
            get { return IEInstance.Div(Find.ByClass("ui-dialog-buttonset")).Button(Find.ByText("No"));  }
        }
        #endregion

        #region RadioButtons
        public RadioButton AddExistingModuleRadioButton
        {
            get { return IEInstance.Div(Find.ById(s => s.EndsWith("RibbonBar_CommonTasksPanel"))).RadioButton(Find.ByValue("AddExistingModule")); }
        }
        #endregion

        #region CheckBoxes
        public CheckBox CopyExistingModuleCheckBox
        {
            get { return CommonTasksDiv.CheckBox(Find.ById(s => s.EndsWith("RibbonBar_AddMod_chkCopyModule"))); }
        }
        #endregion

        #region SelectLists
        public SelectList VisibilitySelectList
        {
            get { return CommonTasksDiv.SelectList(Find.ById(s => s.EndsWith("RibbonBar_AddMod_VisibilityLst"))); }
        }
        public SelectList ModuleCategorySelectList
        {
            get { return CommonTasksDiv.SelectList(Find.ById(s => s.EndsWith("_RibbonBar_AddMod_CategoryList"))); }
        }
        public SelectList ModulePositionSelectList
        {
            get { return CommonTasksDiv.SelectList(Find.ById(s => s.EndsWith("_RibbonBar_AddMod_PositionLst"))); }
        }
        public SelectList AddExistingModulePageSelectList
        {
            get { return CommonTasksDiv.SelectList(Find.ById(s => s.EndsWith("RibbonBar_AddMod_PageLst"))); }
        }
        public SelectList EditPageLocationSelectList
        {
            get { return CurrentPageDiv.SelectList(Find.ById(s => s.EndsWith("RibbonBar_EditPage_LocationLst"))); }
        }
        public SelectList EditPageLocationPageSelectList
        {
            get { return CurrentPageDiv.SelectList(Find.ById(s => s.EndsWith("RibbonBar_EditPage_PageLst"))); }
        }
        #endregion

        #region Spans
        public Span PinUnpinSpan
        {
            get { return IEInstance.Span(Find.ById(s => s.EndsWith("RibbonBar_cmdDock"))); }
        }
        #endregion

        #endregion

        #region Public Methods

        public Link GetRibbonBarLinkByText(string linkText)
        {
            return AdminMenusDiv.Link(Find.ByText(s => s.Contains(linkText)));
        }

        public Element GetItemFromTelerikComboBox(string comboBox, Div comboBoxDiv, string SelectListClass, string ItemText)
        {
            //Click Drop down
            if (comboBox.CompareTo("Module") == 0)
            {
                ModuleComboBox.Click();
            }
            //else if (comboBox.CompareTo("Insert") == 0)
            //{
            //    InsertComboBox.Click();
            //}
            else if(comboBox.CompareTo("ExistingPage") == 0)
            {
                AddExistingModulePageComboBox.ClickNoWait();
            }
            else if (comboBox.CompareTo("ExistingModule") == 0)
            {
                AddExistingModuleComboBox.Click();
            }
            //else if (comboBox.CompareTo("Visibility") == 0)
            //{
            //    ModuleVisibilityComboBox.Click();
            //}
            else if (comboBox.CompareTo("Skin") == 0)
            {
                PageSkinComboBox.Click();
            }
            else if (comboBox.CompareTo("Move Location") == 0)
            {
                MoveLocationComboBox.Click();
            }
            else if (comboBox.CompareTo("Move Page") == 0)
            {
                MovePageComboBox.Click();
            }
            else if (comboBox.CompareTo("Site") == 0)
            {
                SwitchSiteComboBox.Click();
            }
            System.Threading.Thread.Sleep(1500);
            //Find Item to Select
            //Find all List Item elements.
            ElementCollection SelectListElements = comboBoxDiv.Elements.Filter(Find.ByClass(SelectListClass));
            Element result = null;
            //Search for the desired Element
            foreach (Element e in SelectListElements)
            {
                if (e.InnerHtml.Contains(ItemText))
                {
                    //Select the Element
                    result = e;
                    break;
                }
                continue;
            }
            return result;
        }
        
        public void SelectItemFromSelectList(int num, string SelectListID)
        {
            //Find and click the drop down Image
            Image dropDownImage = IEInstance.Image(Find.ById(SelectListID));
            dropDownImage.Click();

            Element selectItem = IEInstance.Div(Find.ByClass("rcbScroll")).Elements.Filter(Find.ByClass("rcbItem"))[num];

        }
        
        public void AddModuleToPage(string moduleName, string moduleType, string moduleCategory)
        {
            ModuleCategorySelectList.Select(moduleCategory);
            System.Threading.Thread.Sleep(1500);
            Element item = null;
            item = GetItemFromTelerikComboBox("Module", ModuleSelectDiv, "rcbItem ", moduleType);
            if(item == null)
            {
                item = GetItemFromTelerikComboBox("Module", ModuleSelectDiv, "rcbHovered ", moduleType);
            }
            item.FireEvent("onmouseover");
            System.Threading.Thread.Sleep(1000);
            item.ClickNoWait();
            if (!moduleName.Equals(""))
            {
                ModuleTitleField.Value = moduleName;
            }
            System.Threading.Thread.Sleep(2000);
            AddModuleButton.ClickNoWait();
        }

        public void AddHTMLModuleToPage(string moduleName, string moduleType)
        {
            ModuleCategorySelectList.Select("All Categories");
            System.Threading.Thread.Sleep(1500);
            Element item = null;
            //Class will be "rcbHovered ", instead of "rcbList "
            item = GetItemFromTelerikComboBox("Module", ModuleSelectDiv, "rcbHovered ", moduleType);
            item.FireEvent("onmouseover");
            item.ClickNoWait();
            if (!moduleName.Equals(""))
            {
                ModuleTitleField.Value = moduleName;
            }
            System.Threading.Thread.Sleep(1000);
       
            AddModuleButton.ClickNoWait();
        }

        public void AddHTMLModuleToPage(string moduleName, string moduleType, string visiblity)
        {
            if(moduleType.Equals("HTML Pro"))
            {
                ModuleCategorySelectList.Select("Professional");
            }
            else
            {
                ModuleCategorySelectList.Select("All Categories");
            }
            System.Threading.Thread.Sleep(1500);
            Element item = null;
            //Class will be "rcbHovered ", instead of "rcbList "
            item = GetItemFromTelerikComboBox("Module", ModuleSelectDiv, "rcbHovered ", moduleType);
            item.FireEvent("onmouseover");
            item.ClickNoWait();
            if (!moduleName.Equals(""))
            {
                ModuleTitleField.Value = moduleName;
            }
            System.Threading.Thread.Sleep(1000);
            VisibilitySelectList.Select(visiblity);
            AddModuleButton.ClickNoWait();
        }

        public void AddHTMLModuleToPage(string moduleName, string moduleType, string visibility, string modulePostion)
        {
            ModuleCategorySelectList.Select("All Categories");
            System.Threading.Thread.Sleep(1500);
            Element item = null;
            //Class will be "rcbHovered ", instead of "rcbList "
            item = GetItemFromTelerikComboBox("Module", ModuleSelectDiv, "rcbHovered ", moduleType);
            item.FireEvent("onmouseover");
            item.ClickNoWait();
            if (!moduleName.Equals(""))
            {
                ModuleTitleField.Value = moduleName;
            }
            System.Threading.Thread.Sleep(1000);
            ModulePositionSelectList.Select(modulePostion);
            AddModuleButton.ClickNoWait();
        }

        public void AddHTMLModuleToPageWithContent(string moduleName, string moduleType, string content, int moduleIndex)
        {
            //Element item = null;
            //item = GetItemFromTelerikComboBox("Module", ModuleSelectDiv, "rcbItem ", moduleType);
            //item.FireEvent("onmouseover");
            //item.Click();
            //ModuleTitleField.Value = moduleName;
            //System.Threading.Thread.Sleep(1000);
            //AddModuleButton.ClickNoWait();
            AddHTMLModuleToPage(moduleName, moduleType);
            ModulePage modulePage = new ModulePage(this);
            //Write some text in the module
            System.Threading.Thread.Sleep(1000);
            modulePage.AddContentToModule(content, moduleIndex);
     
        }

        public void AddHtmlModuleToPageSameAsPageVisibility(string moduleName, string moduleType, string content, int moduleIndex)
        {
            AddHTMLModuleToPage(moduleName, moduleType, "Same As Page");

            ModulePage modulePage = new ModulePage(this);
            //Write some text in the module
            System.Threading.Thread.Sleep(2000);
            modulePage.AddContentToModule(content, moduleIndex); ;
        }

        public void AddHtmlModuleToPagePageEditorsOnlyVisibility(string moduleName, string moduleType, string content, int moduleIndex)
        {
            //Element item = null;
            //item = GetItemFromTelerikComboBox("Module", ModuleSelectDiv, "rcbItem ", moduleType);
            //System.Threading.Thread.Sleep(2000);
            //item.FireEventNoWait("onmouseover");
            //item.Click();
            //if (!moduleName.Equals(""))
            //{
            //    ModuleTitleField.Value = moduleName;
            //}
            ////item = GetItemFromTelerikComboBox("Visibility", VisibilitySelectDiv, "rcbItem ", "Page Editors Only");
            ////item.FireEventNoWait("onmouseover");
            ////item.Click();
            ////System.Threading.Thread.Sleep(1000);

            //VisibilitySelectList.Select("Page Editors Only");
            //AddModuleButton.ClickNoWait();
            AddHTMLModuleToPage(moduleName, moduleType, "Page Editors Only");

            ModulePage modulePage = new ModulePage(this);
            //Write some text in the module
            System.Threading.Thread.Sleep(1500);
            modulePage.AddContentToModule(content, moduleIndex);
        }

        public void AddExistingModuleToPage(string pageName, string moduleName, string moduleListItemClass)
        {
            AddExistingModuleRadioButton.ClickNoWait();
            //Element item = GetItemFromTelerikComboBox("ExistingPage", ExistingPageSelectDiv, "rcbItem ", pageName);
            //item.FireEventNoWait("onmouseover");
            //item.Click();
            AddExistingModulePageSelectList.Select(pageName);
            System.Threading.Thread.Sleep(2000);

            Element item = GetItemFromTelerikComboBox("ExistingModule", ExistingModuleSelectDiv, moduleListItemClass, moduleName);
            item.FireEvent("onmouseover");
            item.Click();
            System.Threading.Thread.Sleep(1000);
            AddModuleButton.ClickNoWait();
        }

        #endregion
    }
}
